package com.example.gitstudy.annoation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hwj
 * @date 2022/12/26 8:37
 * @描述；
 */
public class CheckPointParamsHandlar<T> {

    public Class  clazz;

    private  Field[] fields;

    private T t;

    public CheckPointParamsHandlar(T t) {
        this.clazz = t.getClass();
        // //getFields  只返回公共字段，即有public修饰的字段
//        this.fields = t.getClass().getFields();
        this.fields = t.getClass().getDeclaredFields();
        this.t = t;
    }



    //判断已经注解的字段是否为空
    public ValidResult getParamChecksResults(){
        List<String> list = new ArrayList<>();
        ValidResult res =  new ValidResult();
        Boolean flag = true;
        StringBuffer sb = new StringBuffer();
        for(Field field : fields ){

            //System.out.println(field);
            //private java.lang.String com.zczy.test.bean.Person.userName
            //private java.lang.String com.zczy.test.bean.Person.id
            //private java.lang.String com.zczy.test.bean.Person.sex
            //查看注解的必要字段不能为空--如果没有注解直接跳过
            Annotation annotation=null;
            annotation = field.getAnnotation(ParamsCheck.class);
            if(annotation == null){
                continue;
            }
            //判断注解过的字段是否为空

//            System.out.println("annotation>>>>>"+annotation);
            //annotation>>>>>@com.zczy.test.annoation.ParamsCheck(attributeName=userName)
            //annotation>>>>>@com.zczy.test.annoation.ParamsCheck(attributeName=id)
            String name = field.getName();
            // System.out.println(name);
            //            userName
            //            id
            //            sex
            char[] tempChar = name.toCharArray();
            tempChar[0]  -= 32;
            String tempStr ="get"+ String.valueOf(tempChar);
            list.add(tempStr);

            //System.out.println("list>>>>>"+list);
            // list>>>>>[getUserName, getId]
        }
        for(String str : list){
            try {
                Method method = clazz.getMethod(str);
                //System.out.println("method>>>"+method);
                // method>>>public java.lang.String com.zczy.test.bean.Person.getUserName()
                //method>>>public java.lang.String com.zczy.test.bean.Person.getId()
                Object invoke = method.invoke(t);
//                System.out.println(invoke);
                if(null == invoke || "".equals(invoke)){
                    flag = false;
                    sb.append(str.toLowerCase()+"不能为空").append(",");
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        if(flag == false){
            res =  new ValidResult(sb.toString(),"1");
        }else{
            res =  new ValidResult("校验成功!","0");
        }

        return  res;
    }


}
